namespace chatroom{
    class chat_session_Table{
        public:
            using Ptr = std::shared_ptr<chat_session_Table>;
            chat_session_Table(std::shared_ptr<odb::core::database>& db):_db(db){}
            // 新增会话
            bool insert(chat_session& session)
            { 
                try{
                    //开启事务
                    odb::transaction t(_db->begin());
                    _db->persist(session);
                    t.commit();
                }catch(std::exception& e){
                    Logger_ERROR("插入会话失败{},{}",session.get_session_id(),e.what());
                    return false;
                }
                return true;
            }
            // 删除会话
            bool remove(const std::string& ssid)
            {
                try{
                    //开启事务
                    odb::transaction t(_db->begin());
                    typedef odb::query<chatroom::chat_session> query;
                    typedef odb::result<chatroom::chat_session> result;
                    _db->erase_query<chat_session>(query::session_id==ssid);
                    typedef odb::query<chatroom::ChatSessionMember> query;
                    _db->erase_query<ChatSessionMember>(query::session_id == ssid);
                    t.commit();
                }catch(std::exception& e){
                    Logger_ERROR("通过会话ID{}删除会话失败,{}",ssid,e.what());
                    return false;
                }
                return true;
            }

            bool remove(const std::string &uid, const std::string &pid) {
                //单聊会话的删除，-- 根据单聊会话的两个成员
                try{
                    //开启事务
                    odb::transaction t(_db->begin());
                    typedef odb::query<chatroom::SingalSession> query;
                    typedef odb::result<chatroom::SingalSession> result;
                    auto ret = _db->query_one<SingalSession>(query::CSM1::user_id == uid
                                                            && query::CSM2::user_id == pid
                                                            && query::css::session_type == ChatSessionType::SINGAL);
                    std::string ssid = ret->session_id;
                    typedef odb::query<chatroom::chat_session> query;
                    _db->erase_query<chatroom::chat_session>(query::session_id == ssid);

                    typedef odb::query<chatroom::ChatSessionMember> query;
                    _db->erase_query<chatroom::ChatSessionMember>(query::session_id == ssid);
                    t.commit();
                }catch(std::exception& e){
                    Logger_ERROR("删除单聊会话失败{}-{},{}",uid,pid,e.what());
                    return false;
                }
                return true;
            }
          
            std::shared_ptr<chat_session> select(const std::string &ssid) 
            {
                std::shared_ptr<chat_session> res;
                try{
                    //开启事务
                    odb::transaction t(_db->begin());
                    typedef odb::query<chatroom::chat_session> query;
                    typedef odb::result<chatroom::chat_session> result;
                    res.reset(_db->query_one<chatroom::chat_session>(query::session_id == ssid));
                    t.commit();
                }catch(std::exception& e){
                    Logger_ERROR("通过会话ID查询失败",ssid,e.what());
                    return res;
                }
                return res;
            }
            // 通过用户 ID 获取所有的好友单聊会话（连接会话成员表和用户表）
            std::vector<SingalSession> singleChatSession(const std::string &uid)
            {
                std::vector<SingalSession> res;
                try{
                    //开启事务
                    odb::transaction t(_db->begin());
                    typedef odb::query<chatroom::SingalSession> query;
                    typedef odb::result<chatroom::SingalSession> result;
                    result r(_db->query<chatroom::SingalSession>(query::CSM1::user_id == uid
                                                            &&query::CSM2::user_id != uid
                                                            && query::css::session_type == ChatSessionType::SINGAL));
                    for(auto i(r.begin()); i != r.end(); ++i)
                    {
                        res.push_back(*i);
                    }
                    t.commit();
                }catch(std::exception& e){
                    Logger_ERROR("通过用户ID{}查询所有自己的单聊会话失败,{}",uid,e.what());
                    return res;
                }
                return res;

            }
            // 通过用户 ID 获取所有自己的群聊会话（连接会话成员表和用户表）
            std::vector<GroupSession> groupChatSession(const std::string &uid)
            {
                 std::vector<SingalSession> res;
                try{
                    //开启事务
                    odb::transaction t(_db->begin());
                    typedef odb::query<chatroom::GroupSession> query;
                    typedef odb::result<chatroom::GroupSession> result;
                    result r(_db->query<chatroom::GroupSession>(query::CSM::user_id == uid
                                                            && query::css::session_type == ChatSessionType::GROUP));
                    for(auto i(r.begin()); i != r.end(); ++i)
                    {
                        res.push_back(*i);
                    }
                    t.commit();
                }catch(std::exception& e){
                    Logger_ERROR("通过用户ID{}查询所有自己的群聊会话失败,{}",uid,e.what());
                    return res;
                }
                return res;
            }
        private:
            std::shared_ptr<odb::core::database> _db
    };
}